<?php

namespace app\oauth\controller;

use app\oauth\BaseController;
use think\facade\View;

class Index extends BaseController
{
	public function index()
	{
		$oauthLink = [];
		$oauthList = getOauthConfig();
		foreach ($oauthList  as $_plat => $_config) {
			$redirect_uri = urlencode(getFullDomain() . "/oauth/callback?plat=" . $_plat);
			$link = '';
			$_plat = strtolower($_plat);
			switch ($_plat) {
				case  'gitee':
					$link = "https://gitee.com/oauth/authorize?client_id=" . $_config['id'] . "&redirect_uri={$redirect_uri}&response_type=code";
					break;
				case  'qq':
					$redirect_uri = urlencode(getFullDomain() . "/callback.php?plat=" . $_plat);
					$link = "https://graph.qq.com/oauth2.0/authorize?client_id=" . $_config['id'] . "&redirect_uri={$redirect_uri}&response_type=code&state=" . sha1(rand(100000, 999999));
					break;
				case  'oschina':
					$link = "https://www.oschina.net/action/oauth2/authorize?client_id=" . $_config['id'] . "&redirect_uri={$redirect_uri}&response_type=code";
					break;
				case  'github':
					$link = "https://github.com/login/oauth/authorize?client_id=" . $_config['id'] . "&redirect_uri={$redirect_uri}&response_type=code";
					break;
				default:
					break;
			}
			array_push($oauthLink, [
				'plat' => $_plat,
				'config' => $_config,
				'oauth' => $link
			]);
		}
		View::assign('list', $oauthLink);
		return View::fetch();
	}
}
